package com.tms.control;

import com.tms.domain.Family;
import com.tms.service.ToolDefService;
import com.tms.utils.Data;
import com.tms.utils.JsonResult;
import com.tms.utils.token.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.tms.core.constants.FamilyConstants.*;

/**
 * Tdef的family控制类
 */
@RestController
@RequestMapping("/api/families")
public class FamilyController extends BaseController {

    @Autowired
    private ToolDefService toolDefService;

    @Autowired
    private TokenService tokenService;

    @GetMapping
    public JsonResult getAllFamily(HttpServletRequest request) {
        int workcellId = tokenService.getWorkcellId(request);
        if (workcellId == 0) {
            return new JsonResult(ERROR_TOKEN.getStatus(), ERROR_TOKEN.getDesc());
        }
        List<Family> families = toolDefService.findAllFamilies(workcellId);
        return new JsonResult(SUCCESS.getStatus(), SUCCESS.getDesc(), new Data("families", families));
    }
}
